package com.xuecheng.ucenter.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.ucenter.feignclient.CheckCodeClient;
import com.xuecheng.ucenter.mapper.XcUserMapper;
import com.xuecheng.ucenter.mapper.XcUserRoleMapper;
import com.xuecheng.ucenter.model.dto.RegisterParamsDto;
import com.xuecheng.ucenter.model.po.XcUser;
import com.xuecheng.ucenter.model.po.XcUserRole;
import com.xuecheng.ucenter.service.LoginService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * @author shiminghui
 * @date 2024/9/10 19:24
 * @description: TODO
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class LoginServiceImpl implements LoginService {

    private final CheckCodeClient checkCodeClient;
    private final XcUserMapper xcUserMapper;
    private final PasswordEncoder passwordEncoder;
    private final XcUserRoleMapper xcUserRoleMapper;

    @Override
    @Transactional
    public void register(RegisterParamsDto registerParamsDto) {

        String checkcodekey = registerParamsDto.getCheckcodekey();
        String checkcode = registerParamsDto.getCheckcode();
        if (StringUtils.isBlank(checkcode) || StringUtils.isBlank(checkcodekey)) {
            throw new RuntimeException("验证码无效");
        }
        Boolean verify = checkCodeClient.verify(checkcodekey, checkcode);
        if (!verify) {
            throw new RuntimeException("验证码校验失败");
        }

        String password = registerParamsDto.getPassword();
        String confirmpwd = registerParamsDto.getConfirmpwd();
        if (StringUtils.isBlank(password) || StringUtils.isBlank(confirmpwd)) {
            throw new RuntimeException("密码不能为空");
        }
        if (!password.equals(confirmpwd)) {
            throw new RuntimeException("两次密码不一样");
        }

        String username = registerParamsDto.getUsername();
        XcUser xcUser = xcUserMapper.selectOne(new LambdaQueryWrapper<XcUser>().eq(XcUser::getUsername, username));
        if (xcUser != null) {
            throw new RuntimeException("用户名已存在");
        }
        xcUser = new XcUser();
        //保存数据
        String userId = UUID.randomUUID().toString();
        BeanUtil.copyProperties(registerParamsDto, xcUser);
        xcUser.setName(registerParamsDto.getNickname());
        xcUser.setId(userId);
        xcUser.setStatus("1");
        xcUser.setCreateTime(LocalDateTime.now());
        xcUser.setPassword(passwordEncoder.encode(password));
        xcUser.setUtype("101001");//学生类型
        xcUserMapper.insert(xcUser);

        XcUserRole xcUserRole = new XcUserRole();
        xcUserRole.setId(UUID.randomUUID().toString());
        xcUserRole.setUserId(userId);
        xcUserRole.setRoleId("17");//学生角色
        xcUserRole.setCreateTime(LocalDateTime.now());
        xcUserRoleMapper.insert(xcUserRole);

    }
}
